<?php

namespace common\util\assert;

use common\enums\ResponseCode;
use common\exception\AssertInvalidArgumentException;
use function array_values;

class AssertUtil
{
    /**
     * 如果变量不是空的，返回异常
     *
     * @param $value
     * @param ResponseCode $responseCode
     * @return void
     */
    public static function isEmpty($value, ResponseCode $responseCode)
    {
        if (!empty($value)) {
            static::reportInvalidArgument($responseCode);
        }
    }


    /**
     * 如果变量是空的，返回异常
     *
     * @param $value
     * @param ResponseCode $responseCode
     * @return void
     */
    public static function notEmpty($value, ResponseCode $responseCode)
    {
        if (empty($value)) {
            static::reportInvalidArgument($responseCode);
        }
    }

    /**
     * 如果变量是空数组，返回异常
     *
     * @param $array
     * @param ResponseCode $responseCode
     */
    public static function notEmptyList($array, ResponseCode $responseCode)
    {
        static::isList($array, $responseCode);
        static::notEmpty($array, $responseCode);
    }

    /**
     * 如果变量不是数组，返回异常
     *
     * @param $array
     * @param ResponseCode $responseCode
     */
    public static function isList($array, ResponseCode $responseCode)
    {
        if ($array !== array_values($array)) {
            static::reportInvalidArgument($responseCode);
        }
    }

    /**
     * 如果数值大于限制，返回异常
     *
     * @param int|float $value
     * @param int|float $limit
     * @param ResponseCode $responseCode
     */
    public static function notGreaterThan(int|float $value, int|float $limit, ResponseCode $responseCode)
    {
        if ($value > $limit) {
            static::reportInvalidArgument($responseCode);
        }
    }

    /**
     * 如果数值大于或等于限制，返回异常
     *
     * @param int|float $value
     * @param int|float $limit
     * @param ResponseCode $responseCode
     */
    public static function notGreaterThanOrEqual(int|float $value, int|float $limit, ResponseCode $responseCode)
    {
        if ($value >= $limit) {
            static::reportInvalidArgument($responseCode);
        }
    }

    /**
     * 如果变量不等于指定值，返回异常
     *
     * @param $value
     * @param $comparingValue
     * @param ResponseCode $responseCode
     */
    public static function beEqualTo($value, $comparingValue, ResponseCode $responseCode)
    {
        if ($value !== $comparingValue) {
            static::reportInvalidArgument($responseCode);
        }
    }


    /**
     * @param ResponseCode $responseCode
     * @return void
     * @throw AssertInvalidArgumentException
     */
    protected static function reportInvalidArgument(ResponseCode $responseCode): void
    {
        throw new AssertInvalidArgumentException($responseCode);
    }

}
